//
//  CRContactsController.m
//  CRChatRoom
//
//  Created by CuiFengshuai on 15/11/17.
//  Copyright © 2015年 SecondGroup. All rights reserved.
//

#import "CRContactsController.h"
#import "Friend.h"
#import "FriendGroup.h"
#import "HeaderView.h"
#import "FriendCell.h"
#include "CRTool.h"
#import "CRDialogueViewController.h"
#import "MJRefresh.h"
#import "CRDataTool.h"

/**
 *  界面进来先加载本地缓存的好友列表==如果没有缓存-用户手动刷新之后就会缓存新的数据到本地
 */

@interface CRContactsController () <HeaderViewDelegate, UISearchBarDelegate>

@property (nonatomic, strong) NSArray *groups;

/**
 *  搜索框框
 */
@property (nonatomic, weak) UISearchBar *search;

@end

@implementation CRContactsController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 每一行cell的高度
    self.tableView.rowHeight = 50;
    // 每一组头部控件的高度
    self.tableView.sectionHeaderHeight = 44;
    
    // 去除分割线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    // 集成刷新
    [self setupRefresh];
    
    // 展示缓存中的好友列表
    [self showFriensListFromCache];
    
    //    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"添加好友" style:UIBarButtonItemStyleDone target:self action:@selector(clickAdd)];
    
    // 添加 好友的搜索框
    [self setupSearchBar];
    
    // 界面进来就刷新
    [self.tableView.mj_header beginRefreshing];
    
    
}

// 添加 好友的搜索框
- (void)setupSearchBar
{
    UISearchBar *search = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 300, 30)];
    search.placeholder = @"添加好友...";
    search.returnKeyType = UIReturnKeySearch;
    search.delegate = self;
    self.search = search;
    self.navigationItem.titleView = search;
    
}

// 展示缓存中的好友列表
- (void)showFriensListFromCache
{
    
    // 获得当前用户缓存在本地的好友列表
    NSArray *dictArray = [CRDataTool getFriendsFromCache];
    NSMutableArray *groupArray = [NSMutableArray array];
    for (NSDictionary *dict in dictArray) {
        FriendGroup *group = [FriendGroup groupWithDict:dict];
        [groupArray addObject:group];
        
    }
    _groups = groupArray;
    [self.tableView reloadData];
    
    
}

// 集成刷新
- (void)setupRefresh
{
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headRefresh)];
    self.tableView.mj_header = header;
}

#pragma mark - 下拉刷新调用的方法
- (void)headRefresh
{
    // 获取到当前用户的好友列表信息
    [CRTool getAllFriendsWithBlock:^(NSArray *data) {
        
        NSArray *dictArray = data;
        NSMutableArray *groupArray = [NSMutableArray array];
        for (NSDictionary *dict in dictArray) {
            FriendGroup *group = [FriendGroup groupWithDict:dict];
            [groupArray addObject:group];
            
        }
        _groups = groupArray;
        [self.tableView reloadData];
        // 结束刷新
        [self.tableView.mj_header endRefreshing];
        
    }];
    
}

#pragma mark - 隐藏状态栏
- (BOOL)prefersStatusBarHidden
{
    return YES;
}


#pragma mark - 数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.groups.count;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    FriendGroup *group = self.groups[section];
    return (group.isOpened ? group.friends.count : 0);;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 1.创建cell
    FriendCell *cell = [FriendCell cellWithTableView:tableView];
    
    // 2.设置cell的数据
    FriendGroup *group = self.groups[indexPath.section];
    cell.friendData = group.friends[indexPath.row];
    
    return cell;
}
/**
 *  点击cell 跳转到聊天界面
 *
 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CRDialogueViewController *dialogueVC = [[CRDialogueViewController alloc] init];
    
    FriendGroup *group = self.groups[indexPath.section];
    Friend *friend = group.friends[indexPath.row];
    
    dialogueVC.friendModel = friend;
    
    dialogueVC.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:dialogueVC animated:YES];
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    // 1.创建头部控件
    HeaderView *header = [HeaderView headerViewWithTableView:tableView];
    
    header.delegate = self;
    
    // 2.给header设置数据(给header传递模型)
    header.group = self.groups[section];
    
    return header;
}


#pragma mark - headerView的代理方法
/**
 *  点击了headerView上面的名字按钮时就会调用
 */
- (void)headerViewDidClickedNameView:(HeaderView *)headerView
{
    [self.tableView reloadData];
    [self.search resignFirstResponder];
}

#pragma mark - SearchBar 的代理方法
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    // 在此添加好友===
    [CRTool addFriendWithUsername:searchBar.text];
    
    
}


@end
